package util
{
	import mx.collections.ArrayCollection;
	
	import vo.userInfoVo;

	public class srUtil
	{
		/////////////////////////////////////////////
		// 거치 
		/////////////////////////////////////////////
		public static var iNowYear:int=0;
		
		// 거치 복리
		public static function srUnredeemedCompound(_pv:Number, _n:Number, _ir:Number, periodDv:String="Y", nowYear:int=0):ArrayCollection
		{
			var acSrTargetFv:ArrayCollection = new ArrayCollection();
			var pv:Number 	= _pv; 
			var ir:Number 	= _ir * 0.01;
			var fv:int;
			var iYear:int;				
			var iMonth:int = 0;
			
			var iNowYear:int = nowYear;
			
			if(periodDv=='M') {
				iYear 	= _n/12;
				iMonth 	= _n%12;
			}else{
				iYear = _n;
			}
			
			var calc1:Number = pv;
			var dataObj:Object;
			for(var i:int=1; i<iYear+1; i++) {
				calc1 = calc1 * (1+ir);
				dataObj = {};
				dataObj.year = i+ iNowYear;					
				dataObj.val = Math.round(calc1);
				acSrTargetFv.addItem(dataObj);
				//trace('목표자금 FV 복리 : '  + calc1)
			}
			
			// 월이 있을경우 
			if(iMonth > 0) {			
				var q:Number = Math.pow(1+ir,iMonth/12);
				calc1 = - q * calc1*-1 - (((-1 + q) * 0 * (1 + ir * 0))/ir);					
				dataObj = {};					
				dataObj.month = iMonth;
				dataObj.year = i;					
				dataObj.val = Math.round(calc1);
				
				if(acSrTargetFv.length == 0) {
					acSrTargetFv.addItem(dataObj);
				} else {
					acSrTargetFv.setItemAt(dataObj, acSrTargetFv.length-1)
				}
				//var obj:Object = acSrTargetFv.getItemAt(acSrTargetFv.length-1);
				//dataObj.val = dataObj.val + obj.val;
				//obj = dataObj;					
			}
			return acSrTargetFv;
		}
		
		
		
		//거치 단리
		public static function srUnredeemedSimple(_pv:Number, _n:Number, _ir:Number, periodDv:String="Y"):ArrayCollection
		{
			// 거치 단리 계산 				
			var iYear:int;				
			var iMonth:int = 0;
			
			iYear 	= _n /12;
			iMonth 	= _n % 12;
			
			var iyForM:Number 	= _ir/100/12;
			var dataObj:Object;
			var acSrTargetFv:ArrayCollection = new ArrayCollection();
			var val1:Number = 0;
			var val2:Number = 0;
			for(var i:int=1; i<_n+1; i++) {
				val1 = i*iyForM ;
				val2 = (roundOff(val1) * _pv ) + _pv;
				
				if(i%12==0){
					dataObj = {};
					dataObj.year = i/12;					
					dataObj.val = Math.round(val2);
					acSrTargetFv.addItem(dataObj);
				}
				trace(val2);
			}
			//var viewMonth:Number = roundOff ((_n % 12)*0.1);
			if(iMonth > 0){
				dataObj = {};
				dataObj.month = iMonth;  
				dataObj.year = iYear;// + viewMonth;					
				dataObj.val = Math.round(val2);
				
				if(acSrTargetFv.length == 0) {
					acSrTargetFv.addItem(dataObj);
				} else {
					acSrTargetFv.setItemAt(dataObj, acSrTargetFv.length-1)
				}							
			}
			
			return acSrTargetFv;
		}
		
		
		/*
		public static function srUnredeemed(_pv:Number, _n:Number, _ir:Number):ArrayCollection
		{
			// 상단 거치에만 적용하면 하단은 자동으로 반영됨
			
			if(iNowYear==0){
				var dNow:Date = new Date();
				iNowYear = dNow.fullYear;	
			}
			
			var acSrTargetFv:ArrayCollection = new ArrayCollection();
			var pv:Number 	= _pv;
			var ir:Number 	= _ir * 0.01;// 0.05;
			var n:Number 	= _n;// 15;
			var fv:Number;
			
			var calc1:Number = pv;
			var dataObj:Object;
			for(var i:int=1; i<n+1; i++) {
				calc1 = calc1 * (1+ir);
				dataObj = {};
				dataObj.year = iNowYear+i;					
				dataObj.val = Math.round(calc1);
				acSrTargetFv.addItem(dataObj);
				//trace('목표자금 FV 복리 : '  + calc1)
			}
			return acSrTargetFv;
		}
		*/
		/////////////////////////////////////////////
		// pmt복리
		/////////////////////////////////////////////
		public static function srPmtCompound(ir:Number, calc1:Number, n:Number, m:int=0):ArrayCollection
		{
			
			var acSrPlan1:ArrayCollection = new ArrayCollection();
			var dataObj:Object;
			var pmtMonth:Number = calc1;
			var bStartPmtCheck:Boolean = true;
			// 년구하기
			for(var i:int=1; i<n+1; i++) {
				// 1년 12개월에 대한 Loop
				for(var i2:int=1; i2<13; i2++) {
					if(bStartPmtCheck==true) {
						// 기말 모드 반영 값
						//calc1 = pmtMonth;
						
						////////////////////////////
						// 기시 반영 값
						calc1 = (calc1 * (1+ir));
						////////////////////////////
						bStartPmtCheck = false;
					}else{
						////////////////////////////
						// 기시 반영 값
						 calc1 = ( (calc1+pmtMonth) * (1+ir)) ;
						 
						////////////////////////////
						 // 기말
						//calc1 = (calc1 * (1+ir)) + pmtMonth;
					}																	
				}
				dataObj = {};
				dataObj.year = i;					 
				dataObj.val = Math.round(calc1);
				acSrPlan1.addItem(dataObj);
				//trace('pmt FV 복리 : '  + Math.round(calc1));
			}
			
			////////////////////////////////////
			// 월 구하기 
			////////////////////////////////////
			if(m > 0) {
				for(var m1:int=0; m1<m; m1++) {
					if(bStartPmtCheck==true) {
						////////////////////////////
						// 기말
						//calc1 = pmtMonth;
						
						////////////////////////////
						// 기시 반영 값
						calc1 = (calc1 * (1+ir));
						
						bStartPmtCheck = false;
					}else{
						////////////////////////////
						// 기시 반영 값
						calc1 = ( (calc1+pmtMonth) * (1+ir)) ;
						
						////////////////////////////
						// 기말
						//calc1 = (calc1 * (1+ir)) + pmtMonth;
					}																	
				}
				dataObj = {};
				dataObj.year = i;					
				dataObj.val = Math.round(calc1);
				
				if(acSrPlan1.length == 0) {
					acSrPlan1.addItem(dataObj);
				} else {
					acSrPlan1.setItemAt(dataObj, acSrPlan1.length-1);	
				}
				
			}

			return acSrPlan1;
		}
		/***
		 * pmt 단리 
		 */ 
		public static function srPmtSimple2(ir:Number, calc1:Number, n:Number, m:int=0):ArrayCollection
		{
			var acSrPlan2:ArrayCollection = new ArrayCollection();
			var dataObj:Object;
			var pmtMonth:Number = calc1;
			var iYearMonth:int=0;
			var pmtSum:Number=0;
			var calc2:Number;
			var bStartPmtCheck:Boolean = true;
			for(var i:int=1; i<n+1; i++) {
				iYearMonth = i * 12;
				calc1 = pmtMonth * (( iYearMonth * ( iYearMonth +1 ) ) / 2 )*ir;
				pmtSum = pmtMonth * iYearMonth;
				calc2 = Math.round(calc1 + pmtSum);
				trace('pmt FV 단리 : '  + calc2 );
				
				dataObj = {};
				dataObj.year = i;					
				dataObj.val = Math.round(calc2);
				acSrPlan2.addItem(dataObj);
				
				//trace('pmt FV 단리 : '  + (calc1 + pmtSum) );
			} 
			
			////////////////////////////////////
			// 월 구하기 
			////////////////////////////////////
//			var lastVal:Number = 0;
//			if(acSrPlan2.length > 0){
//				lastVal = acSrPlan2.getItemAt(acSrPlan2.length-1).val; 	 
//			}
			if(m > 0) {
				m = m + (n * 12);
				calc1 = pmtMonth * (( m * ( m +1 ) ) / 2 )*ir;
				pmtSum = pmtMonth * m;
				calc2 = calc1 + pmtSum ;
				trace('pmt FV 단리 : '  + calc2 );
				
				dataObj = {};
				dataObj.year = i;					
				dataObj.val = Math.round(calc2);
				
				if(acSrPlan2.length == 0) {
					acSrPlan2.addItem(dataObj);
				} else {
					acSrPlan2.setItemAt(dataObj, acSrPlan2.length-1);	
				}
			}
			return acSrPlan2;
		}
		 
		
		/////////////////////////////////////////////
		// pmt 단리 srPmtSimple
		//=(10 * (((1년*12)*((1년*12)+1))/2))*(5%/12)
		/////////////////////////////////////////////
		// 현재 사용안함 
		public  function srPmtSimple(ir:Number, calc1:Number, n:Number):ArrayCollection
		{
			var acSrPlan1:ArrayCollection = new ArrayCollection();
			var dataObj:Object;
			var pmtMonth:Number = calc1;
			var bStartPmtCheck:Boolean = true;
			var calc1Sum:Number = 0;
			var calc2:Number=0;
			
			var pmtRM:Number=pmtMonth * ir;
			for(var i:int=1; i<n+1; i++) {
				
				// 1년 12개월에 대한 Loop
				for(var i2:int=1; i2<13; i2++) {	
					
					if(bStartPmtCheck==true) {
						
						calc2 = pmtMonth;
						////////////////////////////
						// 기말시 반영 값
						// calc1 = (calc1 * (1+ir));
						////////////////////////////
						bStartPmtCheck = false;
					}else{
						calc2 =  calc2 + pmtMonth + pmtRM;
						//						calc2 =  calc2 + pmtMonth + pmtRM;
						//calc1 = (calc1 * (1+ir)) + pmtMonth;
						
						////////////////////////////
						// 기말시 반영 값
						// calc1 = ( (calc1+pmtMonth) * (1+ir)) ;
						////////////////////////////
						
					}																	
				}
				calc1Sum = calc1Sum+(calc2+pmtMonth);
				dataObj = {};
				dataObj.year = i;					
				dataObj.val = Math.round(calc2);
				acSrPlan1.addItem(dataObj);
				//trace('pmt FV 복리 : '  + Math.round(calc1));
			} 
			return acSrPlan1;
		}
		
		
		
		public static function roundOff(valueN:Number, dplaces:Number=2):Number{
			var value:String=valueN.toString()
			var returnVal:Number;
			if((value.indexOf(".")!=-1)&&(value.length>(value.indexOf(".")+dplaces))){
				var three:String=value.substring(value.indexOf(".")+dplaces+1,value.indexOf(".")+dplaces+2)
				var one:String=value.substring(0,value.indexOf(".")+dplaces)
				var two:String=value.substring(value.indexOf(".")+dplaces,value.indexOf(".")+dplaces+1)
				if(parseInt(three)>=5){
					value=one+(parseInt(two)+1);
					returnVal=parseFloat(value);						
				}
				else{
					value=one+two;
					returnVal=parseFloat(value)
				}
			} else {
				returnVal = valueN;
			}
			return returnVal;
		} 
		
	}
}